import { useState, useCallback, useRef } from 'react';
import type { Room } from '../../../types/room';
import { RoomStatus } from '../../../types/room';

export interface ContextMenuState {
  visible: boolean;
  x: number;
  y: number;
  room: Room | null;
  date: string | null;
  roomStatus?: RoomStatus;
}

interface UseContextMenuReturn {
  contextMenu: ContextMenuState;
  showContextMenu: (
    event: React.MouseEvent,
    room: Room,
    date: string,
    roomStatus?: RoomStatus
  ) => void;
  hideContextMenu: () => void;
  handleContextMenuAction: (actionId: string, room: Room, date: string) => void;
}

export const useContextMenu = (
  onRoomAction?: (action: string, room: Room, date: string, data?: any) => void
): UseContextMenuReturn => {
  const [contextMenu, setContextMenu] = useState<ContextMenuState>({
    visible: false,
    x: 0,
    y: 0,
    room: null,
    date: null,
  });

  const timeoutRef = useRef<NodeJS.Timeout | null>(null);

  // 显示右键菜单
  const showContextMenu = useCallback(
    (
      event: React.MouseEvent,
      room: Room,
      date: string,
      roomStatus?: RoomStatus
    ) => {
      event.preventDefault();
      event.stopPropagation();

      // 清除之前的定时器
      if (timeoutRef.current) {
        clearTimeout(timeoutRef.current);
      }

      const x = event.clientX;
      const y = event.clientY;

      setContextMenu({
        visible: true,
        x,
        y,
        room,
        date,
        roomStatus,
      });

      console.log(`显示右键菜单: ${room.roomNumber} - ${date}`, {
        位置: { x, y },
        房间状态: roomStatus,
      });
    },
    []
  );

  // 隐藏右键菜单
  const hideContextMenu = useCallback(() => {
    setContextMenu(prev => ({
      ...prev,
      visible: false,
    }));

    // 延迟清理状态，保持动画效果
    timeoutRef.current = setTimeout(() => {
      setContextMenu({
        visible: false,
        x: 0,
        y: 0,
        room: null,
        date: null,
      });
    }, 150);
  }, []);

  // 处理右键菜单操作
  const handleContextMenuAction = useCallback(
    (actionId: string, room: Room, date: string) => {
      console.log(`执行右键菜单操作: ${actionId}`, {
        房间: room.roomNumber,
        日期: date,
        操作: actionId,
      });

      // 根据操作类型执行不同的逻辑
      switch (actionId) {
        case 'book':
          // 新建预订
          if (onRoomAction) {
            onRoomAction('book', room, date, {
              action: '新建预订',
              message: `为房间 ${room.roomNumber} 在 ${date} 新建预订`,
            });
          }
          break;

        case 'modify':
          // 修改预订
          if (onRoomAction) {
            onRoomAction('modify', room, date, {
              action: '修改预订',
              message: `修改房间 ${room.roomNumber} 在 ${date} 的预订`,
            });
          }
          break;

        case 'cancel':
          // 取消预订
          if (onRoomAction) {
            onRoomAction('cancel', room, date, {
              action: '取消预订',
              message: `取消房间 ${room.roomNumber} 在 ${date} 的预订`,
              confirm: true,
            });
          }
          break;

        case 'checkin':
          // 办理入住
          if (onRoomAction) {
            onRoomAction('checkin', room, date, {
              action: '办理入住',
              message: `为房间 ${room.roomNumber} 办理入住手续`,
            });
          }
          break;

        case 'checkout':
          // 办理退房
          if (onRoomAction) {
            onRoomAction('checkout', room, date, {
              action: '办理退房',
              message: `为房间 ${room.roomNumber} 办理退房手续`,
            });
          }
          break;

        case 'extend':
          // 延长住宿
          if (onRoomAction) {
            onRoomAction('extend', room, date, {
              action: '延长住宿',
              message: `延长房间 ${room.roomNumber} 的住宿时间`,
            });
          }
          break;

        case 'maintenance':
          // 设为维护
          if (onRoomAction) {
            onRoomAction('maintenance', room, date, {
              action: '设为维护',
              message: `将房间 ${room.roomNumber} 设为维护状态`,
            });
          }
          break;

        case 'available':
          // 解除维护
          if (onRoomAction) {
            onRoomAction('available', room, date, {
              action: '解除维护',
              message: `将房间 ${room.roomNumber} 设为可用状态`,
            });
          }
          break;

        case 'clean':
          // 标记清洁
          if (onRoomAction) {
            onRoomAction('clean', room, date, {
              action: '标记清洁',
              message: `标记房间 ${room.roomNumber} 为已清洁`,
            });
          }
          break;

        case 'details':
          // 房间详情
          if (onRoomAction) {
            onRoomAction('details', room, date, {
              action: '房间详情',
              message: `查看房间 ${room.roomNumber} 的详细信息`,
            });
          }
          break;

        case 'copy':
          // 复制设置
          if (onRoomAction) {
            onRoomAction('copy', room, date, {
              action: '复制设置',
              message: `复制房间 ${room.roomNumber} 的设置`,
            });
          }
          break;

        case 'history':
          // 查看历史
          if (onRoomAction) {
            onRoomAction('history', room, date, {
              action: '查看历史',
              message: `查看房间 ${room.roomNumber} 的历史记录`,
            });
          }
          break;

        case 'vip':
          // 标记VIP
          if (onRoomAction) {
            onRoomAction('vip', room, date, {
              action: '标记VIP',
              message: `将房间 ${room.roomNumber} 标记为VIP`,
            });
          }
          break;

        case 'today':
          // 定位到今天
          if (onRoomAction) {
            onRoomAction('today', room, date, {
              action: '定位到今天',
              message: '定位到今天的日期',
            });
          }
          break;

        case 'jump':
          // 跳转到日期
          if (onRoomAction) {
            onRoomAction('jump', room, date, {
              action: '跳转到日期',
              message: '跳转到指定日期',
            });
          }
          break;

        default:
          console.warn(`未知的右键菜单操作: ${actionId}`);
      }
    },
    [onRoomAction]
  );

  return {
    contextMenu,
    showContextMenu,
    hideContextMenu,
    handleContextMenuAction,
  };
};
